package com.lefevre.cms.web.action.message;

import com.lefevre.cms.bean.message.Remind;
import com.lefevre.cms.service.message.RemindService;
import com.lefevre.cms.utils.UUIDUtil;
import com.lefevre.cms.utils.Verification;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 提醒管理
 */
@Component("remindManage")
public class RemindManage {

    @Resource
    RemindService remindService;

    /**
     * 取得提醒Id的用户Id(后N位)
     *
     * @param remindId 提醒Id
     * @return
     */
    public int getRemindId(String remindId) {
        String after_userId = remindId.substring(remindId.length() - 4);
        return Integer.parseInt(after_userId);
    }

    /**
     * 生成提醒Id
     * 提醒Id由32位uuid+1位用户Id后4位组成
     *
     * @param userId 用户Id
     * @return
     */
    public String createRemindId(Long userId) {
        //选取得后N位用户Id
        String afterUserId = String.format("%04d", userId % 10000);
        return UUIDUtil.getUUID32() + afterUserId;
    }

    /**
     * 校验提醒Id
     * 提醒Id要先判断是否36位并且最后4位是数字
     *
     * @param remindId 提醒Id
     * @return
     */
    public boolean verificationRemindId(String remindId) {
        if (remindId != null && !"".equals(remindId.trim())) {
            if (remindId.length() == 36) {
                String after_userId = remindId.substring(remindId.length() - 4);
                return Verification.isPositiveIntegerZero(after_userId);
            }
        }
        return false;
    }

    /**
     * 生成提醒对象
     *
     * @return
     */
    public Object createRemindObject(Remind remind) {
        return remind;
    }


    /**
     * 查询缓存 查询未读提醒数量
     *
     * @param userId 用户Id
     * @return
     */
    @Cacheable(value = "remindManage_cache_findUnreadRemindByUserId", key = "#userId")
    public Long query_cache_findUnreadRemindByUserId(Long userId) {
        return remindService.findUnreadRemindByUserId(userId);
    }

    /**
     * 删除缓存 查询未读提醒数量
     *
     * @param userId 用户Id
     * @return
     */
    @CacheEvict(value = "remindManage_cache_findUnreadRemindByUserId", key = "#userId")
    public void delete_cache_findUnreadRemindByUserId(Long userId) {
    }

}
